package com.gome.han.bigdata.mr._002_wordcount;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.CombineTextInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
* @author hanpeng
* @version 创建时间：2020年11月21日 下午3:56:13
* @ClassName 类名称
* @Description 类描述
*/
public class WordcountDriver {
	public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {


		//C:\Users\hadoop\Desktop\data\input\hello.txt
		//C:/Users/hadoop/Desktop/data/input/hello.txt
		Configuration conf = new Configuration();

		//conf.setBoolean("mapreduce.map.output.compress", true);
		//conf.setClass("mapreduce.map.output.compress.codec", BZip2Codec.class, CompressionCodec.class);
		// 1 获取Job对象
		Job job = Job.getInstance(conf);

		// 2 设置jar存储位置
		job.setJarByClass(WordcountDriver.class);

		// 3 关联Map和Reduce类
		job.setMapperClass(WordcountMapper.class);
		job.setReducerClass(WordcountReducer.class);

		// 4 设置Mapper阶段输出数据的key和value类型
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(IntWritable.class);

		// 5 设置最终数据输出的key和value类型
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(IntWritable.class);
		//6  设置输入和输出路径
		FileInputFormat.setInputPaths(job, new Path( WordcountDriver.class.getResource("hello.txt").getPath()));
		//FileInputFormat.setInputPaths(job, new Path( PathTest.class.getResource("").getPath()+"hello.txt"));
		FileOutputFormat.setOutputPath(job, new Path("e:/output/output18"));
		job.setInputFormatClass(CombineTextInputFormat.class);
		CombineTextInputFormat.setMaxInputSplitSize(job, 20971520);
		job.setNumReduceTasks(2);
		// 7 提交job
		// job.submit();
		//这个方法是阻塞的
		boolean result = job.waitForCompletion(true);

		System.exit(result ? 0 : 1);
	}
}
